package com.zp.drug.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.api.bo.DrugInfoBo;
import com.zp.drug.api.vo.DrugInfoVo;
import com.zp.drug.service.IDrugInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 药品字典基本信息控制器
 * 前端访问路由地址为:/drug/drugInfo
 *
 * @author zhang peng
 * @ date 2023-06-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drugInfo")
public class DrugInfoController extends BaseController {

    private final IDrugInfoService iDrugInfoService;

    /**
     * 查询药品字典基本信息列表 (分页)
     */
    @SaCheckPermission("base:drugInfo:list")
    @GetMapping("/selectPage")
    public TableDataInfo<DrugInfoVo> selectPage (DrugInfoBo bo, PageQuery pageQuery) {
        return iDrugInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询药品字典基本信息列表 (不分页)
     */
    @SaCheckPermission("base:drugInfo:list")
    @GetMapping("/list")
    public R<List<DrugInfoVo>> list (DrugInfoBo bo) {
        List<DrugInfoVo> list = iDrugInfoService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出药品字典基本信息列表
     */
    @SaCheckPermission("base:drugInfo:export")
    @Log(title = "药品字典基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (DrugInfoBo bo, HttpServletResponse response) {
        List<DrugInfoVo> list = iDrugInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品字典基本信息", DrugInfoVo.class, response);
    }

    /**
     * 获取药品字典基本信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:drugInfo:list")
    @GetMapping("/{id}")
    public R<DrugInfoVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iDrugInfoService.queryById(id));
    }

    /**
     * 新增药品字典基本信息
     */
    @SaCheckPermission("base:drugInfo:add")
    @Log(title = "药品字典基本信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody DrugInfoBo bo) {
        return toAjax(iDrugInfoService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改药品字典基本信息
     */
    @SaCheckPermission("base:drugInfo:edit")
    @Log(title = "药品字典基本信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody DrugInfoBo bo) {
        return toAjax(iDrugInfoService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品字典基本信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("base:drugInfo:remove")
    @Log(title = "药品字典基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iDrugInfoService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @GetMapping("/refreshCacheDrugInfo")
    public R<String> refreshCacheDrugInfo () {
        iDrugInfoService.refreshCache();
        return R.ok("刷新成功");
    }
}
